from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework import status

from utils.logging_utils import log_request_response
from .models import UserFeedback
from .serializers import UserFeedbackSerializer
from rest_framework.permissions import IsAuthenticated
from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework.decorators import api_view, authentication_classes, permission_classes


@api_view(['POST'])
@authentication_classes([JWTAuthentication])
@permission_classes([IsAuthenticated])
@log_request_response
def create_user_feedback(request):
    # 将请求数据复制为一个可变字典，以便添加用户信息
    data = request.data.copy()
    # 自动将 user 设为当前登录用户的 id
    data['user'] = request.user.id

    # 在创建序列化器时传入 context，包括 request
    serializer = UserFeedbackSerializer(data=data, context={'request': request})
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
